package com.android.resetfile.frame;

import com.android.resetfile.controller.Controller;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;

import javax.swing.*;
import java.io.InputStream;

/**
 * Created by Administrator on 14-10-5.
 */
public class IndexFrame extends JFrame {
    private  JFXPanel chartFxPanel;
    private static IndexFrame frame;
    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                } catch (Exception e) {}
                frame= new IndexFrame();
                frame.initFrame();
            }
        });
    }

    private  void initFrame() {
       setUndecorated(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        setBounds(200,200,513,313);
        chartFxPanel = new JFXPanel();
        //调用fx的thread
        Platform.runLater(new Runnable() {
            public void run() {
                gotoLogin(chartFxPanel);
            }
        });
        setContentPane(chartFxPanel);
    }
    private void gotoLogin(JFXPanel chartFxPanel) {
        Controller login = null;
        try {
            login = (Controller) replaceSceneContent("index.fxml",chartFxPanel);
            login.init(this);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private Initializable replaceSceneContent(String fxml,JFXPanel chartFxPanel) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = Main.class.getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(Main.class.getResource(fxml));
        Pane page;
        try {
            page = (Pane) loader.load(in);
        } finally {
            in.close();
        }
        Scene scene = new Scene(page, 513,  313.0);
        chartFxPanel.setScene(scene);
        Initializable initializable = loader.getController();

        return initializable;
    }

}
